package com.neu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.neu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000121623825";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQCDrufOX2PwO0lyL9gw2dJ7i8VCtouGGdmy5bfMYE4V4lbyYNATe7V25xjuK5QgEksVYr69bs3SdNZTxwwyrYIJAlkuVQavzsaR/ygNzUhwS23vfjqBHHy/jkfVJnLAVRyT156MIZFKway10sc7S9MWy01C2iGHmDsE+19FCQzGnwbsrVR5vIel/VpJvf0VZPXowTrjzfkTWRQRJbHGt4kPCaXwMgeaRh1iUO77xIV9w0ug0S43bPgJC1atSpgA095cmE5DjgsMpc0pPHF36vA5PTZDeTBzThx2QAZcRxdsDtso1COoBSAfH2JPCieC07l33FDskzzIidf2am5eQn/bAgMBAAECgf9o7vfJfDonNRdR5s5gHzObP8XxY4XLVs4jwOq18ghHaBXhD0TAQdRY687x+8gNxoYOhKF6UtGw42nnWLZfq+PzSevG//pUHnId5TYFyY/v7CuurOfx+sFt4/NZ50mJ+kjLhwbQo+8jozH9sfkRR1ruzxLCMxCE/MoLLkS8vnlvzCS6d5E98XFDlMIuqxbhXyCp/v0YFm3V0hT9mTWrVANXVh5GeuiHd/s1hcZoY2SvStrLAoRtj90Wooxnt4bfrIQKhBNfg9MzeYBQlK5FNjjc3o2uP+KuG/gERI8LuUKiG3ohxxqK8axPoBtPCzPoDweYay7CM3eL1o+8Myy4miECgYEAwRbgArvyxMF8eFlVUmalycNF9hzPykghbwIu252g3CK4tJTjzFWBVp/MPkxR3qw//KdD/g9hUaOYS14LbzuVC0ZtJS6y2+tWWEvTvjkGDBIRTl9vfyN05cPqSRqsvo15DwAI/RwSGO56+WdnzZl8F0BRJsG4xOKZ/VaYe5PLGvUCgYEArpZKcDliAB90TihYAxeUfuzbE7kX4RYyuqDvDkuNCPIm9FqOAEbOLM+wobbFuYzjQlGQkDwGQ7KgrSQ/Pp4Xbw1YMMAJXer5Pena5uIqjDde1aHGeO35Btox4UE/nC+xRFRlNTQL2vLHBdmkOBdyERUZP0c8EVA4gM6NCruwDY8CgYBW/Qw8jxnm5FhEC9LxhYcJJ60cHIzMT8SoMy6TCCeRJR9bU3xw8NCNCv7DzjRQAf/tvhlWmu04D2CsC5lRKmGSK88+IhS4GQTUUIz7NMtxoq4jRcGAYKPoY2PoPVk3m5q/VD+4b+4DR2UleR19tbj+7J3ivQGVyZSgWmPlwIaPBQKBgEytJw44tYjWR69X2qepAIfqIKF95jXuGOOMcNF3bv6h02f4KWuHrV9S7AFqE1bVwK3MwtAF/L5Ywmaoh7pJloMoejYW8UY1P7focEJ8zDD+1IDA5zkPUxKmW8fRb016SMF4y2ZdcGN9wtkX6B7WdTt5Z7g54fC8qcQwcxNmsQCTAoGBAIofV5juMQGXje1pQdUPqN5YNFSEvi0gOm1t3JvjsuXeTuWdN4MHqpAfwtA4qS6ga4k3iY5sLUrfCTA8N/7uTbcxlfVyGL+8E0wXZUu+Uh9DTFGzFcMO6M1Cns3gSbPeL9PGd6byRR4oZi6JPVk4iCw/JS2m8IG2sNjsNxJxKJW5";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAo1/XwGSupAg9pCBrSmToYGRB7+5HcxCUaXLxQoZtPYAjM2N6Z/L62qz1+YwUiwYIaCyyP3TKypt8C3kQOyS79RT4PmA7KrbDAqPr4KLYBWkNuEPInR52fApg2dsokdXtbaNc22DCf53E5Evz/NDLp6lOEl158DPf5govpZv4KH1yb/v6a9QoFIE0+mYZQeVAlKGsS7LnPdssOwyot9d3fTpFzONvvLoSJsPe9nNFhZsb4VLmJX1NdGs40RDbIOZ3r54o+jk9wjjau1SYnJTW3ywFcv2Ayf4/Tpllvzk1PoZEmziVwT2w8m6Y6S9+1OnspB6sAqJMxemVrn+9Wxm5tQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://order.gulimall.com:8081/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://order.gulimall.com:8081/memberOrder.html";

    // 签名方式
    public String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
